package com.property.manage.service.parking;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.property.manage.dao.parking.ParkingRentDeductionMapper;
import com.property.manage.model.parking.ParkingRentDeductionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/9/14
 */
@Component
public class ParkingRentDeductionService {

    private ParkingRentDeductionMapper parkingRentDeductionMapper;



    public Optional<ParkingRentDeductionEntity> find(Integer villageId, Integer year, Integer month) {
        QueryWrapper<ParkingRentDeductionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("village_id", villageId);
        wrapper.eq("year", year);
        wrapper.eq("month", month);

        return Optional.ofNullable(parkingRentDeductionMapper.selectOne(wrapper));
    }


    public void add(Integer villageId, Integer year, Integer month) {

        ParkingRentDeductionEntity deductionEntity = new ParkingRentDeductionEntity();
        deductionEntity.setVillageId(villageId);
        deductionEntity.setYear(year);
        deductionEntity.setMonth(month);
        deductionEntity.setState(ParkingRentDeductionEntity.STATE_DEDUCTING);
        deductionEntity.setCreateTime(LocalDateTime.now());

        parkingRentDeductionMapper.insert(deductionEntity);
    }

    public void complete(Integer villageId, Integer year, Integer month) {
        UpdateWrapper<ParkingRentDeductionEntity> wrapper = new UpdateWrapper<>();

        wrapper.eq("village_id", villageId);
        wrapper.eq("year", year);
        wrapper.eq("month", month);

        wrapper.set("state", ParkingRentDeductionEntity.STATE_DEDUCTED);

        parkingRentDeductionMapper.update(null, wrapper);
    }


    @Autowired
    public void setParkingRentDeductionMapper(ParkingRentDeductionMapper parkingRentDeductionMapper) {
        this.parkingRentDeductionMapper = parkingRentDeductionMapper;
    }
}
